#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int lengthOfLongestSubstring(char* s) {
    int len = strlen(s);
    int left = 0;
    int right = 0;
    int max = 0;
    int has_same = 0;
    int i = 0;
    int j = 0;

    for(i = 0; i < len; i++) {
        if(left <= right) {
            has_same = 0;
            for(j = left; j < right; j++) {
                if(s[j] == s[right]) {
                    has_same = 1;
                    break;
                }
            }
            if(has_same) {
                // 跳转到最近的一个相同的字符
                left = j + 1;
            }
        }
        max = max < (right - left + 1) ? right - left + 1 : max;
        right++;
    }

    return max;
}

int main(void)
{
    char s1[] = "abcabcbb";
    char s2[] = "bbbbb";
    char s3[] = "pwwkew";
    char s4[] = " ";
    char s5[] = "au";
    char s6[] = "aab";
    char s7[] = "";
    char s8[] = "xxzqi";
    int sub_len = 0;

    sub_len = lengthOfLongestSubstring(s1);
    printf("1 - sub_len = %d\r\n", sub_len);
    
    sub_len = lengthOfLongestSubstring(s2);
    printf("2 - sub_len = %d\r\n", sub_len);

    sub_len = lengthOfLongestSubstring(s3);
    printf("3 - sub_len = %d\r\n", sub_len);

    sub_len = lengthOfLongestSubstring(s4);
    printf("4 - sub_len = %d\r\n", sub_len);

    sub_len = lengthOfLongestSubstring(s5);
    printf("5 - sub_len = %d\r\n", sub_len);

    sub_len = lengthOfLongestSubstring(s6);
    printf("6 - sub_len = %d\r\n", sub_len);

    sub_len = lengthOfLongestSubstring(s7);
    printf("7 - sub_len = %d\r\n", sub_len);

    sub_len = lengthOfLongestSubstring(s8);
    printf("8 - sub_len = %d\r\n", sub_len);
    return 0;
}
